home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyc (Python 2.4) import unittest from test import test_support import zlib import random def getbuf(): import imp try: t = imp.find_module('test_zlib') file = t[0] except ImportError: file = open(__file__) buf = file.read() * 8 file.close() return buf class ChecksumTestCase(unittest.TestCase): def test_crc32start(self): self.assertEqual(zlib.crc32(''), zlib.crc32('', 0)) def test_crc32empty(self): self.assertEqual(zlib.crc32('', 0), 0) self.assertEqual(zlib.crc32('', 1), 1) self.assertEqual(zlib.crc32('', 432), 432) def test_adler32start(self): self.assertEqual(zlib.adler32(''), zlib.adler32('', 1)) def test_adler32empty(self): self.assertEqual(zlib.adler32('', 0), 0) self.assertEqual(zlib.adler32('', 1), 1) self.assertEqual(zlib.adler32('', 432), 432) def assertEqual32(self, seen, expected): self.assertEqual(seen & 0xFFFFFFFFL, expected & 0xFFFFFFFFL) def test_penguins(self): self.assertEqual32(zlib.crc32('penguin', 0), 0xE5C1A120L) self.assertEqual32(zlib.crc32('penguin', 1), 1136044692) self.assertEqual32(zlib.adler32('penguin', 0), 198116086) self.assertEqual32(zlib.adler32('penguin', 1), 198574839) self.assertEqual(zlib.crc32('penguin'), zlib.crc32('penguin', 0)) self.assertEqual(zlib.adler32('penguin'), zlib.adler32('penguin', 1)) class ExceptionTestCase(unittest.TestCase): def test_bigbits(self): self.assertRaises(zlib.error, zlib.compress, 'ERROR', zlib.MAX_WBITS + 1) def test_badcompressobj(self): self.assertRaises(ValueError, zlib.compressobj, 1, zlib.DEFLATED, 0) def test_baddecompressobj(self): self.assertRaises(ValueError, zlib.decompressobj, 0) class CompressTestCase(unittest.TestCase): def test_speech(self): x = zlib.compress(HAMLET_SCENE) self.assertEqual(zlib.decompress(x), HAMLET_SCENE) def test_speech128(self): data = HAMLET_SCENE * 128 x = zlib.compress(data) self.assertEqual(zlib.decompress(x), data) class CompressObjectTestCase(unittest.TestCase): def test_pair(self): data = HAMLET_SCENE * 128 co = zlib.compressobj() x1 = co.compress(data) x2 = co.flush() self.assertRaises(zlib.error, co.flush) dco = zlib.decompressobj() y1 = dco.decompress(x1 + x2) y2 = dco.flush() self.assertEqual(data, y1 + y2) def test_compressoptions(self): level = 2 method = zlib.DEFLATED wbits = -12 memlevel = 9 strategy = zlib.Z_FILTERED co = zlib.compressobj(level, method, wbits, memlevel, strategy) x1 = co.compress(HAMLET_SCENE) x2 = co.flush() dco = zlib.decompressobj(wbits) y1 = dco.decompress(x1 + x2) y2 = dco.flush() self.assertEqual(HAMLET_SCENE, y1 + y2) def test_compressincremental(self): data = HAMLET_SCENE * 128 co = zlib.compressobj() bufs = [] for i in range(0, len(data), 256): bufs.append(co.compress(data[i:i + 256])) bufs.append(co.flush()) combuf = ''.join(bufs) dco = zlib.decompressobj() y1 = dco.decompress(''.join(bufs)) y2 = dco.flush() self.assertEqual(data, y1 + y2) def test_decompinc(self, flush = False, source = None, cx = 256, dcx = 64): if not source: pass source = HAMLET_SCENE data = source * 128 co = zlib.compressobj() bufs = [] for i in range(0, len(data), cx): bufs.append(co.compress(data[i:i + cx])) bufs.append(co.flush()) combuf = ''.join(bufs) self.assertEqual(data, zlib.decompress(combuf)) dco = zlib.decompressobj() bufs = [] for i in range(0, len(combuf), dcx): bufs.append(dco.decompress(combuf[i:i + dcx])) self.assertEqual('', dco.unconsumed_tail, "(A) uct should be '': not %d long" % len(dco.unconsumed_tail)) if flush: bufs.append(dco.flush()) else: while True: chunk = dco.decompress('') if chunk: bufs.append(chunk) continue break self.assertEqual('', dco.unconsumed_tail, "(B) uct should be '': not %d long" % len(dco.unconsumed_tail)) self.assertEqual(data, ''.join(bufs)) def test_decompincflush(self): self.test_decompinc(flush = True) def test_decompimax(self, source = None, cx = 256, dcx = 64): if not source: pass source = HAMLET_SCENE data = source * 128 co = zlib.compressobj() bufs = [] for i in range(0, len(data), cx): bufs.append(co.compress(data[i:i + cx])) bufs.append(co.flush()) combuf = ''.join(bufs) self.assertEqual(data, zlib.decompress(combuf), 'compressed data failure') dco = zlib.decompressobj() bufs = [] cb = combuf while cb: chunk = dco.decompress(cb, dcx) self.failIf(len(chunk) > dcx, 'chunk too big (%d>%d)' % (len(chunk), dcx)) bufs.append(chunk) cb = dco.unconsumed_tail bufs.append(dco.flush()) self.assertEqual(data, ''.join(bufs), 'Wrong data retrieved') def test_decompressmaxlen(self, flush = False): data = HAMLET_SCENE * 128 co = zlib.compressobj() bufs = [] for i in range(0, len(data), 256): bufs.append(co.compress(data[i:i + 256])) bufs.append(co.flush()) combuf = ''.join(bufs) self.assertEqual(data, zlib.decompress(combuf), 'compressed data failure') dco = zlib.decompressobj() bufs = [] cb = combuf while cb: max_length = 1 + len(cb) // 10 chunk = dco.decompress(cb, max_length) self.failIf(len(chunk) > max_length, 'chunk too big (%d>%d)' % (len(chunk), max_length)) bufs.append(chunk) cb = dco.unconsumed_tail if flush: bufs.append(dco.flush()) else: while chunk: chunk = dco.decompress('', max_length) self.failIf(len(chunk) > max_length, 'chunk too big (%d>%d)' % (len(chunk), max_length)) bufs.append(chunk) self.assertEqual(data, ''.join(bufs), 'Wrong data retrieved') def test_decompressmaxlenflush(self): self.test_decompressmaxlen(flush = True) def test_maxlenmisc(self): dco = zlib.decompressobj() self.assertRaises(ValueError, dco.decompress, '', -1) self.assertEqual('', dco.unconsumed_tail) def test_flushes(self): sync_opt = [ 'Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH'] sync_opt = _[1] data = HAMLET_SCENE * 8 for sync in sync_opt: for level in range(10): obj = zlib.compressobj(level) a = obj.compress(data[:3000]) b = obj.flush(sync) c = obj.compress(data[3000:]) d = obj.flush() self.assertEqual(zlib.decompress(''.join([ a, b, c, d])), data, 'Decompress failed: flush mode=%i, level=%i' % (sync, level)) del obj def test_odd_flush(self): import random if hasattr(zlib, 'Z_SYNC_FLUSH'): co = zlib.compressobj(zlib.Z_BEST_COMPRESSION) dco = zlib.decompressobj() try: gen = random.WichmannHill() except AttributeError: try: gen = random.Random() except AttributeError: gen = random except: None<EXCEPTION MATCH>AttributeError None<EXCEPTION MATCH>AttributeError gen.seed(1) data = genblock(1, 17 * 1024, generator = gen) first = co.compress(data) second = co.flush(zlib.Z_SYNC_FLUSH) expanded = dco.decompress(first + second) self.assertEqual(expanded, data, "17K random source doesn't match") def test_empty_flush(self): co = zlib.compressobj(zlib.Z_BEST_COMPRESSION) self.failUnless(co.flush()) dco = zlib.decompressobj() self.assertEqual(dco.flush(), '') def genblock(seed, length, step = 1024, generator = random): '''length-byte stream of random data from a seed (in step-byte blocks).''' if seed is not None: generator.seed(seed) randint = generator.randint if length < step or step < 2: step = length blocks = [] for i in range(0, length, step): []([]([ chr(randint(0, 255)) for x in range(step) ])) return ''.join(blocks)[:length] def choose_lines(source, number, seed = None, generator = random): '''Return a list of number lines randomly chosen from the source''' if seed is not None: generator.seed(seed) sources = source.split('\n') return [ generator.choice(sources) for n in range(number) ] HAMLET_SCENE = "\nLAERTES\n\n O, fear me not.\n I stay too long: but here my father comes.\n\n Enter POLONIUS\n\n A double blessing is a double grace,\n Occasion smiles upon a second leave.\n\nLORD POLONIUS\n\n Yet here, Laertes! aboard, aboard, for shame!\n The wind sits in the shoulder of your sail,\n And you are stay'd for. There; my blessing with thee!\n And these few precepts in thy memory\n See thou character. Give thy thoughts no tongue,\n Nor any unproportioned thought his act.\n Be thou familiar, but by no means vulgar.\n Those friends thou hast, and their adoption tried,\n Grapple them to thy soul with hoops of steel;\n But do not dull thy palm with entertainment\n Of each new-hatch'd, unfledged comrade. Beware\n Of entrance to a quarrel, but being in,\n Bear't that the opposed may beware of thee.\n Give every man thy ear, but few thy voice;\n Take each man's censure, but reserve thy judgment.\n Costly thy habit as thy purse can buy,\n But not express'd in fancy; rich, not gaudy;\n For the apparel oft proclaims the man,\n And they in France of the best rank and station\n Are of a most select and generous chief in that.\n Neither a borrower nor a lender be;\n For loan oft loses both itself and friend,\n And borrowing dulls the edge of husbandry.\n This above all: to thine ownself be true,\n And it must follow, as the night the day,\n Thou canst not then be false to any man.\n Farewell: my blessing season this in thee!\n\nLAERTES\n\n Most humbly do I take my leave, my lord.\n\nLORD POLONIUS\n\n The time invites you; go; your servants tend.\n\nLAERTES\n\n Farewell, Ophelia; and remember well\n What I have said to you.\n\nOPHELIA\n\n 'Tis in my memory lock'd,\n And you yourself shall keep the key of it.\n\nLAERTES\n\n Farewell.\n" def test_main(): test_support.run_unittest(ChecksumTestCase, ExceptionTestCase, CompressTestCase, CompressObjectTestCase) if __name__ == '__main__': test_main() def test(tests = ''): if not tests: tests = 'o' testcases = [] if 'k' in tests: testcases.append(ChecksumTestCase) if 'x' in tests: testcases.append(ExceptionTestCase) if 'c' in tests: testcases.append(CompressTestCase) if 'o' in tests: testcases.append(CompressObjectTestCase) test_support.run_unittest(*testcases) if False: import sys sys.path.insert(1, '/Py23Src/python/dist/src/Lib/test') import test_zlib as tz ts = tz.test_support ut = tz.unittest su = ut.TestSuite() su.addTest(ut.makeSuite(tz.CompressTestCase)) ts.run_suite(su)